<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Reset_password extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * Default Reset_password page
	 *
	 * @access	public
	 * @output	reset_password.tpl
	 */
	public function index()
	{		
		$data['title'] = "Forgot your password?";
		if($this->session->flashdata('notice') != '')
			$data['notice'] = $this->session->flashdata('notice');
		$this->parser->parse("reset_password/reset_password.tpl", $data);
	}
	
	/**
	 * send_request
	 *
	 * @access	public
	 * @redirect	Success: 		/reset_password
	 *				Unsuccessful: 	/reset_password
	 */
	public function send_request()
	{
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation', 'email'));
		$this->load->model(array('User_model', 'Passwordreset_model'));

		$this->form_validation->set_rules('username', 'Username', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->session->set_flashdata('notice', 'Please enter a valid username');
		} else {
			$user = $this->User_model->get_user_by_username($this->input->post('username'));

			if($user != NULL)
			{
				/* create request in database */
				$key = md5($user->id.$user->username.$user->email.date('Y-m-d H:i:s'));
				$this->Passwordreset_model->create_password_reset($user->id, $key);
				/* email */
				$this->email->from('support@achieveit.com', 'Achieveit');
				$this->email->to($this->input->post('email')); 
				$this->email->subject('[AchieveIt] Reset your password');
				$this->email->message($key); 
				$this->email->send();
				
				$this->session->set_flashdata('notice', 'Instructions have been sent to your email address on how to reset your password');
			} else {
				$this->session->set_flashdata('notice', "User '" . $this->input->post('username') . "' could not be found, perhaps you misspelt it?");
			}
		}
		redirect('/reset_password');
	}
	
	/**
	 * enter_new_password
	 *
	 * @access	public
	 */
	public function enter_new_password($key = '')
	{
		$this->load->helper(array('url'));
		$this->load->model(array('Passwordreset_model'));
		
		if($key == '')
		{
			$this->session->set_flashdata('notice', 'The key you have entered has expired, please try again');
			redirect('/reset_password');
			return;
		}
		$key = $this->Passwordreset_model->get_password_reset($key);
		if($key == NULL)
		{
			$this->session->set_flashdata('notice', 'The key you have entered has expired, please try again');
			redirect('/reset_password');
			return;
		}
		$data['title'] = "Change your password?";
		$data['key'] = $key->key;
		if($this->session->flashdata('notice') != '')
			$data['notice'] = $this->session->flashdata('notice');
		$this->parser->parse("reset_password/change_your_password.tpl", $data);
	}
	
	/**
	 * change_password
	 *
	 * @access	public
	 */
	public function change_password()
	{
		$this->load->helper(array('url'));
		$this->load->library(array('form_validation'));
		$this->load->model(array('User_model', 'Passwordreset_model'));
		
		$this->form_validation->set_rules('password', 'New Password', 'required');
		$this->form_validation->set_rules('confirmpassword', 'Re-type New Password', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->session->set_flashdata('notice', 'Please enter a new password and re-type it');
			redirect('/reset_password/enter_new_password/' . $this->input->post('key'));
			return;
		}
		
		if($this->input->post('password') != $this->input->post('confirmpassword'))
		{
			$this->session->set_flashdata('notice', 'Password do not match. Please try again');
			redirect('/reset_password/enter_new_password/' . $this->input->post('key'));
			return;
		}

		$key = $this->Passwordreset_model->get_password_reset($this->input->post('key'));
		$user = $this->User_model->update_user_password($key->user_id, $this->input->post('password'));
		$this->Passwordreset_model->delete_password_reset($this->input->post('key'));
		
		$this->session->set_flashdata('notice', 'Your password has been successfully changed');
		
		redirect('/home');
	}
	
}
?>